﻿package net.wangit.dev.settings;

import net.wangit.MapList;
import net.wangit.Path;
import net.wangit.context.ActionContext;
import net.wangit.framework.config.BooleanString;
import net.wangit.framework.config.DomainsSettings;
import net.wangit.framework.config.ProjectSettings;
import net.wangit.support.Action;
import net.wangit.support.MapListFactory;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class ProjectSettingsHelper implements MapListFactory, Action {
	
	public ProjectSettingsHelper() {
	}

	@Override
	public ActionContext execute(ActionContext ac) throws Exception {
		this.saveToXML(ac);
		ProjectSettings.instance().load();
		DomainsSettings.load();
		return ac;
	}

	/**
	 * 保存配置到xml文件
	 * 
	 * @param ac
	 * @throws Exception
	 */
	private void saveToXML(ActionContext ac) throws Exception {
		String path = Path.getConfigPath(ProjectSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			FileUtil.delete(path);
		}
		Document doc = DocumentHelper.createDocument();
		Element el = doc.addElement("project");
		el.addElement("superUserOption").setText(
				ac.getRequestParameter("projectsettings.attr11"));
		el.addElement("logBufferSize").setText(
				ac.getRequestParameter("projectsettings.attr12"));

		el.addElement("trackEnabled").setText(
				String.valueOf(BooleanString.TRUE.equals(ac
						.getRequestParameter("projectsettings.attr21"))));
		el.addElement("recordEnabled").setText(
				String.valueOf(BooleanString.TRUE.equals(ac
						.getRequestParameter("projectsettings.attr22"))));

		el.addElement("reloadI18n").setText(
				String.valueOf(BooleanString.TRUE.equals(ac
						.getRequestParameter("projectsettings.attr31"))));
		el.addElement("reloadTemplate").setText(
				String.valueOf(BooleanString.TRUE.equals(ac
						.getRequestParameter("projectsettings.attr32"))));
		XMLUtil.writeUTF8(doc, path);
	}

	/**
	 * 恢复默信设置
	 * 
	 * @param ac
	 * @return
	 * @throws Exception
	 */
	public ActionContext restore(ActionContext ac) throws Exception {
		String path = Path.getConfigPath(ProjectSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			FileUtil.delete(path);
		}
		ProjectSettings.instance().load();
		return ac;
	}

	@Override
	public MapList getMapList() {
		MapList list = new MapList();
		ProjectSettings pro = ProjectSettings.instance();
		list.put(0, "attr11", pro.getSuperUserOption());
		list.put(0, "attr12", pro.getLogBufferSize());
		list.put(0, "attr21", pro.isTrackEnabled() ? BooleanString.TRUE
				: BooleanString.FALSE);
		list.put(0, "attr22", pro.isRecordEnabled("") ? BooleanString.TRUE
				: BooleanString.FALSE);
		list.put(0, "attr31", pro.isReloadI18n() ? BooleanString.TRUE
				: BooleanString.FALSE);
		list.put(0, "attr32", pro.isReloadTemplate() ? BooleanString.TRUE
				: BooleanString.FALSE);
		return list;
	}
}
